#include "../include/netdisk.h"
// 文件名是否存在
int filesExits(const char *path)
{
    struct stat statbuf;
    if (stat(path, &statbuf) != 0)
    {
        return 0;
    }
    return S_ISREG(statbuf.st_mode);
}
// 目录名是否存在
int directoryExits(const char *path)
{
    struct stat statbuf;
    if (stat(path, &statbuf) != 0)
    {
        return 0;
    }
    return S_ISDIR(statbuf.st_mode);
}
// 解析命令
int commandCheck(char *command, train_t *train)
{
    char *instruction = NULL;
    char *name = NULL;

    instruction = strtok(command, " ,\t\n");
    name = strtok(NULL, " ,\t\n");

    if (name != NULL)
    {
        strcpy(train->data, name);
    }
    train->length = strlen(train->data);
    if (strcmp(instruction, "cd") == 0)
    {
        train->cmdstat = CD;
        if(name == NULL)
        {
            printf("CD命令后请写入正确的目录(路径)名!\n");
            return -1;
        }

    }
    else if (strcmp(instruction, "rmdir") == 0)
    {
        train->cmdstat = RMDIR;
    }
    else if (strcmp(instruction, "mkdir") == 0)
    {
        train->cmdstat = MKDIR;
    }
    else if (strcmp(instruction, "remove") == 0)
    {
        train->cmdstat = REMOVE;
    }
    else if (strcmp(instruction, "gets") == 0)
    {
        train->cmdstat = GETS;
    }
    else if (strcmp(instruction, "puts") == 0)
    {
        train->cmdstat = PUTS;
    }
    else if (strcmp(instruction, "ls") == 0)
    {
        train->cmdstat = LS;
        if (name == NULL)
        {
            memcpy(train->data, ".", 1);
            train->length = strlen(train->data);
        }
    }
    else if (strcmp(instruction, "pwd") == 0)
    {
        if (name == NULL)
        {
            train->cmdstat = PWD;
        }
        else
        {
            printf("PWD命令后不能接参数!\n");
            return -1;
        }
    }
    // 错误命令处理
    else
    {
        printf("命令不合法!\n");
        return -1;
    }
    return 0;
}